package com.leetcode.Offer;

public class JZ49_3 {
    public int nthUglyNumber(int n) {
        int[] dp = new int[n];
        dp[0] = 1;
        int a = 0, b = 0, c = 0;
        for (int i = 1; i < n; i++) {
            int a2 = dp[a] * 2, b3 = dp[b] * 3, c5 = dp[c] * 5;
            dp[i] = Math.min(a2, Math.min(b3, c5));
            if (dp[i] == a2) {
                a++;
            }
            if (dp[i] == b3) {
                b++;
            }
            if (dp[i] == c5) {
                c++;
            }
        }
        return dp[n - 1];
    }
}
